package aom.ju.ss.view.base;


import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import aom.ju.ss.lpInterface.MultiLayoutBindViewInterface;
import aom.ju.ss.mode.PAdapter2Mode;


/**
 * 多布局
 */

public class PAdapter2 extends RecyclerView.Adapter<ListHolder> {

    private int[] layoutId;

    private MultiLayoutBindViewInterface bindViewInterface;

    private List<PAdapter2Mode> list = new ArrayList<>();


    public PAdapter2(List<PAdapter2Mode> mData, MultiLayoutBindViewInterface bindViewInterface, int... layoutId) {
        this.list = mData;
        this.layoutId = layoutId;
        this.bindViewInterface = bindViewInterface;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemViewType(int position) {

        return list.get(position).getType();
    }

    @NotNull
    @Override
    public ListHolder onCreateViewHolder(@NotNull ViewGroup parent, int viewType) {

        ViewDataBinding binding = DataBindingUtil.inflate(
                LayoutInflater.from(parent.getContext()),
                layoutId[viewType],
                parent,
                false);
        ListHolder listHolder = new ListHolder(binding.getRoot());
        listHolder.setBinding(binding);
        return listHolder;
    }



    @Override
    public void onBindViewHolder(@NotNull ListHolder holder, int position) {

        bindViewInterface.bandView(holder, position, getItemViewType(position), list.get(position).getData());
    }


    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public void onAttachedToRecyclerView(@NotNull RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);

        RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
        if (manager instanceof GridLayoutManager) {
            final GridLayoutManager gridManager = ((GridLayoutManager) manager);
            gridManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    return list.get(position).getCon() == 0 ? gridManager.getSpanCount() : list.get(position).getCon();
                }
            });
        }
    }
}

